//
//  Tab_ToHandler.swift
//  mall-ios
//
//  Created by Meak Tsui on 2018/12/5.
//  Copyright © 2018 Meak Tsui. All rights reserved.
//

import Foundation
import MTHybrid
import Shared

class Tab_ToHandler: MTEventHandler {
    var ns: Ns {
        return .Tab
    }
    
    var action: Api {
        return .To
    }
    
    func handler(_ msg: MTHybridMsg) {
        guard let index = msg.params["index"] as? Int else {
            dPrint("index value is nil")
            msg.callbackClosure?(["success":false,"msg":NSLocalizedString("index is null", comment: "index is null")])
            return
        }
        guard let vcCount = msg.vc.tabBarController?.viewControllers?.count,index < vcCount else {
            dPrint("index value is great than tab count")
            msg.callbackClosure?(["success":false,"msg":NSLocalizedString("index is too large", comment: "index is too large")])
            return
        }
        let nav = msg.vc.navigationController
//        nav?.popToRootViewController(animated: false)
        nav?.tabBarController?.selectedIndex = index
        msg.callbackClosure?("null")
    }
    
}
